package fr.upmc.cie;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;

import org.apache.http.conn.ConnectTimeoutException;
import org.xml.sax.SAXException;

import fr.upmc.cie.connexion.Updater;
import fr.upmc.cie.data.CategorieManager;
import fr.upmc.cie.data.ChannelItem;
import fr.upmc.cie.data.CategorieItem;
import fr.upmc.cie.data.DataManager;
import fr.upmc.cie.ext.Utils;
import fr.upmc.cie.xml.AndroidSaxFeedParser;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
public class Subscribe extends Activity{

	private Subscribe self = this;
	private ChannelItem ci ;
	private Spinner spinnerCategorie;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.subscribe);
		
		spinnerCategorie = (Spinner) this.findViewById(R.id.subscribe_spinner_categorie);
		
		CategorieManager categorieManager = DataManager.getCategorieManager(this);
		
		SpinnerAdapter sa = new ArrayAdapter<CategorieItem>(this, android.R.layout.simple_spinner_item, categorieManager.getAllCategories());
		
		spinnerCategorie.setAdapter(sa);
		
		final Button button = (Button) this.findViewById(R.id.subscribe_bt);
		button.setText("Check");
		final EditText tv_title = (EditText) this.findViewById(R.id.subscribe_et_title);
		tv_title.setEnabled(false);
		final EditText tv_url = (EditText) this.findViewById(R.id.subscribe_et_url);
		tv_url.setText("http://");
		button.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				//Tester si URL est valide
				if(!tv_title.isEnabled()){
					ProgressDialog dialog = ProgressDialog.show(self, "", 
	                        "Checking. Please wait...", true);
					//Tester s'il existe de connexion Internet
					if (Utils.IsConnectedToNetwork(self)){
						AndroidSaxFeedParser asfp;
						try {
							//Essayer de parser le fichier xml
							asfp = new AndroidSaxFeedParser(tv_url.getText().toString());
							ci = asfp.parseChannel();
							tv_title.setEnabled(true);
							tv_title.setText(ci.getTitle());
							tv_url.setEnabled(false);
							button.setText("Subscribe");
							dialog.dismiss();
						} catch (MalformedURLException e) {
							Utils.getAlertBox(self, "URL malformed");
						} catch (ConnectTimeoutException e){
							Utils.getAlertBox(self, "Connect time out");
						} catch (UnknownHostException e){
							Utils.getAlertBox(self, "Unknown host name");
						} catch (IOException e) {
							Utils.getAlertBox(self, "Unknown host name");
						} catch (SAXException e) {
							e.printStackTrace();
						} finally {
							dialog.dismiss();
						}
					}else{
						Utils.getAlertBox(self, "Device not connected to network");
						dialog.dismiss();
					}
					
				}else{
					//Au cas URL est valider, on va essayer de sauvegarder channel dans la base
					if(ci!=null){
						ci.setTitle(tv_title.getText().toString());
						ci.setUrl(tv_url.getText().toString());
						boolean res = DataManager.getChannelManager(self).insertChannel(ci, self);
						if (res) {
							//Faire le lien entre channel et categorie
							if(spinnerCategorie.getSelectedItem()!=null)
								DataManager.getChannelManager(self).setChannelCategorie(ci, (CategorieItem) spinnerCategorie.getSelectedItem());
							//Mettre a jour les tickets
							Updater.refresh(self);
							self.setResult(2);
							finish();
						}
					}
					
				}
			}
			
		});
	}
}
